﻿using Cofe.Core.Filter;
using Cofe.Core.Property;
using Cofe.Core.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reactive.Linq;
using Cofe.Core.Interfaces;

namespace Cofe.Core.Entry
{
    [Obsolete]
    public class CustomEntryLinkPropertyProvider : PropertyProviderBase, IEntryLinkPropertyProvider
    {

        #region Constructor

        private CustomEntryLinkPropertyProvider(ICustomEntryListPropertyProvider celPP, string relativeName, string entryPath = null, IPropertyHost entry = null)
            : base(PropertyDefinitions.CofeDefinitions, PropertyDefinitions.CofeEntryLinkDefinitions)
        {
            _relativeName = relativeName;

            if (entryPath == null && entry == null)
                throw new ArgumentException("EntryPath and Entry cannot be both null.");

            if (entry != null)
            {
                _entryPH = entry;
                _entryPath = entryPath ?? entry.Behaviors.GetProperty<string>(CofeProperties.FullParseName);
            }
            else
            {
                _entryPH = null;
                _entryPath = entryPath;
            }

            _celPP = celPP;
            //_parentParsePath = parentParsePath;
            //_name = PathFE.GetFileName(_entryPath);            
            registerProperties(CofeProperties.Name, () => _relativeName, (pp) =>
                {
                    try { _celPP.setName(_relativeName, pp.ValueAsString); _relativeName = pp.ValueAsString; return true; }
                    catch { return false; }
                }
            );
            registerProperties(CofeProperties.Position, () => _celPP.getPosition(_relativeName), (pp) =>
            {
                try { _celPP.setPosition(_relativeName, pp.ValueAsInt32); return true; }
                catch { return false; }
            }
            );
            registerProperties(CofeProperties.FullParseName, () =>
            PathFE.Combine(
                celPP.GetOneProperty<string>(CofeProperties.FullParseName),
                this.GetOneProperty<string>(CofeProperties.Name))
            );
            registerProperties(CofeProperties.DefaultInterfaceType, () => typeof(IEntryLink));
            registerProperties(CofeProperties.Type, () => EntryTypes.Link);
            registerProperties(CofeProperties.ParentParseName, () =>
                celPP.GetOneProperty<string>(CofeProperties.FullParseName));
            registerProperties(CofeProperties.Parent, () =>
                EntryConstructor.ConstructEntry<ICustomEntryList>(
                    EntryConstructor.ConstructPropertyHost(_celPP)));
            registerProperties(CofeEntryLinkProperties.LinkedEntryPH, () =>
                {
                    if (_entryPH == null)
                    {
                        var e = EntryFE.TryParsePathAsync(_entryPath, ParsableType.ParsePath).Result;
                        _entryPH = e.Properties;
                    }
                    return _entryPH;
                });

            registerActionAsync(CofeProperties.DeleteAsync, (pm) => Task.Run(() => { _celPP.remove(_relativeName); pm.IsHandled = true; }));
        }

        public CustomEntryLinkPropertyProvider(ICustomEntryListPropertyProvider celPP, string relativeName, string entryPath)
            : this(celPP, relativeName, entryPath, null)
        {

        }

        public CustomEntryLinkPropertyProvider(ICustomEntryListPropertyProvider celPP, string relativeName, IPropertyHost entry)
            : this(celPP, relativeName, entry.Behaviors.GetProperty<string>(CofeProperties.FullParseName), entry)
        {
            
        }

        #endregion

        #region Methods

        private bool setName(string name)
        {
            return false;
        }

        #endregion

        #region Data

        private ICustomEntryListPropertyProvider _celPP;
        private string _relativeName;
        private string _entryPath = null;
        private IPropertyHost _entryPH = null;
        private IEntryConstructor _entryConstructor = null;

        #endregion

        #region Public Properties

        public IEntryConstructor EntryConstructor
        {
            get { return _entryConstructor ?? CofeServices.ServiceLocater.FindService<IEntryConstructor>(); }
            set { _entryConstructor = value; }
        }


        #endregion


    }
}
